
/**播放背景音乐
 * @param {Object} src
 */
function playAudioBg(src) {
	console.log(common.bgmusic);
	if(!common.isplaymusic || src == common.bgmusic.src){
		return false
	}
	if(common.bgmusic){//ios清空播放
		common.bgmusic.destroy();
	}
	common.bgmusic = uni.createInnerAudioContext(); //创建播放器对象
	common.bgmusic.autoplay = true; //自动播放
	common.bgmusic.loop = true;
	if (src) {
		common.bgmusic.src = src; //选择播放的音频
	}
	common.bgmusic.play(); //执行播放
	common.bgmusic.onPlay(() => {
	});
	common.bgmusic.onError((res) => {
		common.bgmusic.seek(0)
		common.bgmusic.play()
	});
}

/**播放声音
 * @param {Object} src
 */
function playAudio(src) {
	if(!common.isplaymusic){
		return false
	}
	if (common.othermusic) {
		common.othermusic.destroy();
	}
	// #ifdef APP-PLUS  
	
	// 背景音乐文件  
	let path = '';
	if(src == '../../static/audio/click.mp3'){
		path = plus.io.convertLocalFileSystemURL("_www/static/audio/click.mp3"); 
	}else if(src == '../../static/audio/right.mp3'){
		path = plus.io.convertLocalFileSystemURL("_www/static/audio/right.mp3"); 
	}else if(src == '../../static/audio/wrong.mp3'){
		path = plus.io.convertLocalFileSystemURL("_www/static/audio/wrong.mp3"); 
	}else if(src == '../../static/audio/sign.mp3'){
		path = plus.io.convertLocalFileSystemURL("_www/static/audio/sign.mp3"); 
	}else if(src == '../../static/audio/prop.mp3'){
		path = plus.io.convertLocalFileSystemURL("_www/static/audio/prop.mp3"); 
	}
	if (plus.os.name == 'Android') {  
	    let MediaPlayer = plus.android.importClass("android.media.MediaPlayer");  
		if(common.audioPlayer){
		   common.audioPlayer.stop();
		   common.audioPlayer.release();
		   common.audioPlayer=new MediaPlayer();
	    }else{
			common.audioPlayer=new MediaPlayer();
	    }
	    common.audioPlayer.setDataSource(path); //指定音频文件路径  
	    common.audioPlayer.setLooping(false); //设置为循环播放  
	    common.audioPlayer.prepare(); //初始化播放器MediaPlayer  
	    common.audioPlayer.start();  
	    // 获取音乐的总时长  
	    console.log(common.audioPlayer.getDuration());  
		return false
	}  
	// #endif
	common.othermusic = uni.createInnerAudioContext(); //创建播放器对象
	common.othermusic.src = src; //选择播放的音频
	common.othermusic.play(); //执行播放
	common.othermusic.onPlay(() => {
	});
	common.othermusic.onError((res) => {
		console.log(res.errMsg);
		console.log(res.errCode);
	});
	common.othermusic.onEnded((res)=>{
	})
}


export default {
	playAudioBg,
	playAudio
}
